class AddPermissions < ActiveRecord::Migration
  def self.up
    create_table :permission_types do |t|
      t.string :enumeration
      t.string :name
    end
    
    create_table :user_attributes do |t|
      t.string :enumeration
      t.integer :default_permission_id
    end
    
    create_table :user_attribute_settings do |t|
      t.integer :user_id
      t.integer :user_attribute_id
      t.integer :permission_type_id
    end

    permission_type_list = [['PUBLIC', "Public"], ['PRIVATE',"Private"], ['RESTRICTED', "Restricted"]]
    permissions = []
    for permission_enumeration, permission_name in permission_type_list
      permissions << PermissionType.create!(:enumeration => permission_enumeration, :name => permission_name)
    end

    user_attribute_list = [["first_name", "RESTRICTED"], ["last_name", "PRIVATE"], ["email", "PRIVATE"], ["age", "PRIVATE"]]
    

    for attribute, default_permission in user_attribute_list
      UserAttribute.create!(:enumeration => attribute, 
                            :default_permission_id => permissions.find{ |p| p.enumeration == default_permission}.id )
    end


  end

  def self.down
    drop_table :permission_types
    drop_table :user_attributes
    drop_table :user_attribute_settings
  end
end
